﻿using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataInfo;
using KenLib.BusinessManager;
using AjaxControlToolkit; 


public partial class JOINT_frmJ_InterviewResultSearch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((JOINT_MstPage_JOINT)this.Master).HideLiArea("frmJ_TraineeInterviewList");
        ((JOINT_MstPage_JOINT)this.Master).changeTitle("面接結果管理");

		if (Session["人コード"] == null)
		{
			Response.Redirect("../frmD_Login.aspx");
		}
		else if (Session["人コード"].ToString() == "")
		{
			Response.Redirect("../frmD_Login.aspx");
		}

		if (Request.UrlReferrer != null)
		{
			string PageRequest = Request.UrlReferrer.ToString();
			if (PageRequest.IndexOf("frmJ_TraineeManager.aspx") > 0)
			{
				Session.Remove("datatable_search");
				Session.Remove("応募情報");
				Session.Remove("組合選択");
				Session.Remove("組合員企業選択");
				Session.Remove("面接日");
				if (Session["count_paging"] != null && Int32.Parse(Session["count_paging"].ToString()) >= 0)
				{
					for (int i = 0; i <= Int32.Parse(Session["count_paging"].ToString()); i++)
					{
						Session.Remove("arr" + i);
					}
				}
				Session.Remove("count_paging");
			}
		}
		else
		{
			Session.Remove("datatable_search");
			Session.Remove("応募情報");
			Session.Remove("組合選択");
			Session.Remove("組合員企業選択");
			Session.Remove("面接日");
			if (Session["count_paging"] != null && Int32.Parse(Session["count_paging"].ToString()) >= 0)
			{
				for (int i = 0; i <= Int32.Parse(Session["count_paging"].ToString()); i++)
				{
					Session.Remove("arr" + i);
				}
			}
			Session.Remove("count_paging");
		}
        
        if (!IsPostBack)
        {
			if (Session["役"].ToString() == "1") {
				ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('khong cho truy cap'); window.location.href='frmJ_TraineeManager.aspx';", true);
			}

            grv.Columns[3].Visible = false;
            grv.Columns[6].Visible = false;

            Session["paging"] = -1;
            this.getDD1_組合選択();

            string strHeader = "面接番号,氏名,ID番号,ID,生年月日,結果,面接情報ID";
            ShowGridViewNotClassEmpty(grv, strHeader, "データがありません。");

            RequiredFieldValidator組合選択.ErrorMessage = GetError("ER00007");
            RequiredFieldValidator組合員企業選択.ErrorMessage = GetError("ER00007");
            RequiredFieldValidator面接日.ErrorMessage = GetError("ER00001");

            if (Session["応募情報"] != null) 
            {
                DataTable dt_back = (DataTable)Session["応募情報"];
                ddl_組合員企業選択.SelectedValue = (String)Session["組合員企業選択"];
                ddl_組合選択.SelectedValue = (String)Session["組合選択"];
                //ddl_面接日.SelectedValue = (String)Session["面接日"];
                txt_グループ名.Text = dt_back.Rows[0]["グループ名"].ToString();

                本社情報Manager frm = new 本社情報Manager();
                DataTable dt_組合選択 = new DataTable();
                dt_組合選択 = frm.frmJ_TraineeAppAssignConfirm_GetDDl組合員企業選択(Int32.Parse(ddl_組合選択.SelectedValue));
                if (dt_組合選択.Rows.Count > 0)
                {
                    ddl_組合員企業選択.DataSource = dt_組合選択;
                    ddl_組合員企業選択.DataTextField = "企業名";
                    ddl_組合員企業選択.DataValueField = "企業ID";
                    ddl_組合員企業選択.DataBind();

                    ListItem 選択 = new ListItem("[選択]", "-1");
                    ddl_組合員企業選択.Items.Insert(0, 選択);
                }
                Session["paging"] = 0;
                面接情報Manager frm1 = new 面接情報Manager();
                DataTable dt = dt = frm1.frmJ_InterviewResultSearch_面接日(Int32.Parse(ddl_組合選択.SelectedValue), Int32.Parse(ddl_組合員企業選択.SelectedValue));
                if (dt.Rows.Count>0)
                {
                    ddl_面接日.DataSource = dt;
                    ddl_面接日.DataTextField="面接日";
                    ddl_面接日.DataValueField="面接日";
                    ddl_面接日.DataBind();
                    foreach (DataRow row in dt.Rows)
                    {
                        if (Session["面接日"].ToString() == row["面接日"].ToString())
                        {
                            ddl_面接日.SelectedValue = row["面接日"].ToString();
                        }
                    }
                }

                BindData(Int32.Parse(ddl_組合選択.SelectedValue), Int32.Parse(ddl_組合員企業選択.SelectedValue), ddl_面接日.SelectedValue);
            }
            ddl_組合選択.Focus();
        }
    }

    /// <summary>
    /// Load du lieu vao dropdownList tai cot 結果 trong gridview
    /// </summary>
    /// <returns></returns>
    public DataTable Load結果()
    {
        DataTable dt = new DataTable();
        DataColumn col;
        DataRow row;

        col = new DataColumn();
        col.DataType = System.Type.GetType("System.Int32");
        col.ColumnName = "id";
        dt.Columns.Add(col);

        col = new DataColumn();
        col.DataType = System.Type.GetType("System.String");
        col.ColumnName = "name";
        dt.Columns.Add(col);

            //row = dt.NewRow();
            //row["id"] = -1;
            //row["name"] = "[選択]";
            //dt.Rows.Add(row);

            row = dt.NewRow();
            row["id"] = 0;
            row["name"] = "企業面接欠席";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["id"] = 1;
            row["name"] = "企業面接合格";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["id"] = 2;
            row["name"] = "企業面接不合格";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["id"] = 3;
            row["name"] = "予備合格";
            dt.Rows.Add(row);
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "startup();", true);
        return dt;
    }
    protected void btn確認_Click(object sender, EventArgs e)
    {
        DataTable dt_session = new DataTable();
        DataColumn col_session;
        DataRow row_session;

        col_session = new DataColumn();
        col_session.DataType = System.Type.GetType("System.String");
        col_session.ColumnName = "面接番号";
        dt_session.Columns.Add(col_session);

        col_session = new DataColumn();
        col_session.DataType = System.Type.GetType("System.String");
        col_session.ColumnName = "氏名";
        dt_session.Columns.Add(col_session);

        col_session = new DataColumn();
        col_session.DataType = System.Type.GetType("System.String");
        col_session.ColumnName = "ID番号";
        dt_session.Columns.Add(col_session);

        col_session = new DataColumn();
        col_session.DataType = System.Type.GetType("System.String");
        col_session.ColumnName = "ID";
        dt_session.Columns.Add(col_session);

        col_session = new DataColumn();
        col_session.DataType = System.Type.GetType("System.String");
        col_session.ColumnName = "生年月日";
        dt_session.Columns.Add(col_session);

        col_session = new DataColumn();
        col_session.DataType = System.Type.GetType("System.String");
        col_session.ColumnName = "グループ名";
        dt_session.Columns.Add(col_session);

        col_session = new DataColumn();
        col_session.DataType = System.Type.GetType("System.String");
        col_session.ColumnName = "結果";
        dt_session.Columns.Add(col_session);

        col_session = new DataColumn();
        col_session.DataType = System.Type.GetType("System.String");
        col_session.ColumnName = "面接情報ID";
        dt_session.Columns.Add(col_session); 

        DataTable dt_search = (DataTable)Session["datatable_search"];
        int page_size = grv.PageSize;
        int number_session = dt_search.Rows.Count % page_size;

        int k = -1;
        int count = 0;
        for (int i = 0; i < dt_search.Rows.Count; i++)
        {
            row_session = dt_session.NewRow();
            row_session["面接番号"] = dt_search.Rows[i]["面接番号"].ToString();
            row_session["氏名"] = dt_search.Rows[i]["氏名"].ToString();
            row_session["ID番号"] = dt_search.Rows[i]["ID番号"].ToString();
            row_session["ID"] = dt_search.Rows[i]["ID"].ToString();
            row_session["生年月日"] = dt_search.Rows[i]["生年月日"].ToString();
            row_session["面接情報ID"] = dt_search.Rows[i]["面接情報ID"].ToString();
            row_session["グループ名"] = txt_グループ名.Text;

            if (i % page_size == 0) { k++; }

            if (Session["arr" + k] != null)
            {
                int[,] arr2 = new int[900, 900];
                arr2 = (int[,])Session["arr" + k];
                int j = i + 1;
                row_session["結果"] = arr2[k, j].ToString();
                if(arr2[k, j].ToString() == "0")
                    count++;
                else if (arr2[k, j].ToString() == "2")
                    count++;
            }
            else
            {
                row_session["結果"] = "0";
                count++;
            }
            
            dt_session.Rows.Add(row_session);
        }
        if (count == dt_search.Rows.Count) {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('" + GetError("ER00023") + "');", true);
            return;
        }
        
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "startup();", true);
        Session["応募情報"] = dt_session;
        Response.Redirect("frmJ_InterviewResultCheck.aspx");
    }

    /// <summary>
    /// Load du lieu dropdownList 組合選択
    /// </summary>
    public void getDD1_組合選択()
    {
        本社情報Manager frm = new 本社情報Manager();
        DataTable dt = new DataTable();
        dt = frm.Search_TraineeEnterSchool();
        if (dt.Rows.Count > 0)
        {
            ddl_組合選択.DataSource = dt;
            ddl_組合選択.DataTextField = "企業名";
            ddl_組合選択.DataValueField = "企業ID";
            ddl_組合選択.DataBind();

            ListItem 選択 = new ListItem("[選択]", "-1");
            ddl_組合選択.Items.Insert(0, 選択);
        }
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "startup();", true);
    }

    /// <summary>
    /// Load du lieu dropdownList 組合員企業選択
    /// </summary>
    public void getDD1_組合員企業選択()
    {
        本社情報Manager frm = new 本社情報Manager();
        DataTable dt = new DataTable();
        if (ddl_組合選択.SelectedIndex != 0)
        {
            dt = frm.frmJ_TraineeAppAssignConfirm_GetDDl組合員企業選択(Int32.Parse(ddl_組合選択.SelectedValue));
        }
        else
        {
            dt = frm.frmJ_TraineeAppAssignConfirm_GetDDl組合員企業選択(0);
        }

        //if (dt.Rows.Count > 0)
        {
            ddl_組合員企業選択.DataSource = dt;
            ddl_組合員企業選択.DataTextField = "企業名";
            ddl_組合員企業選択.DataValueField = "企業ID";
            ddl_組合員企業選択.DataBind();

            ListItem 選択 = new ListItem("[選択]", "-1");
            ddl_組合員企業選択.Items.Insert(0, 選択);
        }

        if (ddl_組合選択.SelectedValue == "-1") {
			ListItem 選択 = new ListItem("[選択]", "-1");
			ddl_組合員企業選択.Items.Clear();
            ddl_組合員企業選択.Items.Insert(0, 選択);

			ddl_面接日.Items.Clear();
			ddl_面接日.Items.Insert(0, 選択);
        }
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "startup();", true);
    }

	/// <summary>
	/// Load du lieu dropdownList 面接日
	/// </summary>
	public void getDD1_面接日()
    {
		面接情報Manager frm = new 面接情報Manager();
		DataTable dt = new DataTable();
		if (ddl_組合員企業選択.SelectedIndex != 0)
		{
			dt = frm.frmJ_InterviewResultSearch_面接日(Int32.Parse(ddl_組合選択.SelectedValue), Int32.Parse(ddl_組合員企業選択.SelectedValue));
		}
		else
		{
			dt = frm.frmJ_InterviewResultSearch_面接日(0, 0);
		}

		//if (dt.Rows.Count > 0)
		{
			ddl_面接日.DataSource = dt;
			ddl_面接日.DataTextField = "面接日";
			ddl_面接日.DataValueField = "面接日";
			ddl_面接日.DataBind();

			ListItem 選択 = new ListItem("[選択]", "-1");
			ddl_面接日.Items.Insert(0, 選択);
		}

		if (ddl_組合選択.SelectedValue == "-1")
		{
			ddl_面接日.Items.Clear();
			ListItem 選択 = new ListItem("[選択]", "-1");
			ddl_面接日.Items.Insert(0, 選択);
		}
		ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "startup();", true);
	}

    /// <summary>
    /// Hien thi dong text khi gridview rong
    /// </summary>
    /// <param name="grv"></param>
    /// <param name="strTableHeader"></param>
    /// <param name="strEmpty"></param>
    public void ShowGridViewNotClassEmpty(GridView grv, string strTableHeader, string strEmpty)
    {
        DataTable dt = new DataTable();
        string[] arrColumnNames = strTableHeader.Split(',');
        foreach (string str in arrColumnNames)
        {
            dt.Columns.Add(str);
        }
        grv.ShowFooter = false;
        DataRow r = dt.NewRow();
        dt.Rows.InsertAt(r, 0);
        grv.DataSource = dt;
        grv.DataBind();
        grv.Rows[0].Cells.Clear();
        TableCell cell = new TableCell();
        cell.ColumnSpan = grv.Columns.Count + 1;
        cell.Text = strEmpty;
        grv.Rows[0].Cells.Add(cell);
    }
    protected void btn決定_Click(object sender, EventArgs e)
    {
		if (Session["count_paging"] != null && Int32.Parse(Session["count_paging"].ToString()) >= 0)
        {
            for (int i = 0; i <= Int32.Parse(Session["count_paging"].ToString()); i++)
            {
                Session.Remove("arr" + i);
            }
        }

        int 組合選択 = Int32.Parse(ddl_組合選択.SelectedValue);
        int 組合員企業選択 = Int32.Parse(ddl_組合員企業選択.SelectedValue);
        string 面接日 = ddl_面接日.SelectedValue;

        BindData(組合選択, 組合員企業選択, 面接日);

		ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "startup();", true);
    }

    /// <summary>
    /// Hien thi lai du lieu khi click phan trang, dac biet la o cot co dropdownList
    /// </summary>
    /// <param name="組合選択"></param>
    /// <param name="組合員企業選択"></param>
    /// <param name="面接日"></param>
    protected void BindData(int 組合選択, int 組合員企業選択, string 面接日)
    {
		string strHeader = "面接番号,氏名,ID番号,ID,生年月日,結果,面接情報ID";

        応募情報Manager frm = new 応募情報Manager();
        DataTable dt = frm.SelectData応募情報(組合選択, 組合員企業選択, 面接日);
        Session["datatable_search"] = dt;

        Session["count_paging"] = dt.Rows.Count / grv.PageSize;

        if (dt.Rows.Count > 0)
        {
            grv.DataSource = dt;
            grv.DataBind();
            txt_グループ名.Enabled = true;
            Session["組合選択"] = ddl_組合選択.SelectedValue;
            Session["組合員企業選択"] = ddl_組合員企業選択.SelectedValue;
            Session["面接日"] = ddl_面接日.SelectedValue;
            
            if (Session["paging"] != null && Int32.Parse(Session["paging"].ToString()) >= 0)
            {
                int number_page2 = Int32.Parse(Session["paging"].ToString());
                int[,] arr2 = new int[900, 900];
                if (Session["arr" + number_page2] != null)
                {
                    arr2 = (int[,])Session["arr" + number_page2];
                    int page_size = grv.PageSize;
                    int start_count = page_size * number_page2 + 1;

                    for (int i = 0; i < grv.Rows.Count; i++)
                    {
                        GridViewRow row2 = grv.Rows[i];
                        DropDownList ddl2 = ((DropDownList)row2.FindControl("ddl_結果"));
                        ddl2.SelectedValue = arr2[number_page2, start_count].ToString();
                        start_count++;
                    }
                    
                }
            }
        }
        else
        {
            ShowGridViewNotClassEmpty(grv, strHeader, "データがありません。");
			txt_グループ名.Text = "";
            txt_グループ名.Enabled = false;
            txt_グループ名.Focus();
        }
    }

    protected void grv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Finding the Dropdown control.
            Control ctrl = e.Row.FindControl("ddl_結果");
            if (ctrl != null)
            {
                DropDownList dd = ctrl as DropDownList;
                //Binding the Dropdown with Dummy data.
                dd.DataTextField = "name";
                dd.DataValueField = "id";
                dd.DataSource = Load結果();
                dd.DataBind();
            }
        }
    }
    protected void grv_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Finding the Dropdown control.
            Control ctrl = e.Row.FindControl("ddl_結果");
            if (ctrl != null)
            {
                DropDownList dd = ctrl as DropDownList;
                //Binding the Dropdown with Dummy data.
                dd.DataTextField = "name";
                dd.DataValueField = "id";
                dd.DataSource = Load結果();
                dd.DataBind();
            }
        }
    }
    protected void grv_SelectedIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv.PageIndex = e.NewPageIndex;
        Session["paging"] = e.NewPageIndex;
        BindData(Int32.Parse(Session["組合選択"].ToString()), Int32.Parse(Session["組合員企業選択"].ToString()), Session["面接日"].ToString());
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "startup();", true);
    }

    /// <summary>
    /// Luu gia tri da chon cua dropdown o cot 結果 trong gridview
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void SelectChange(object sender, EventArgs e)
    {
        if (Int32.Parse(Session["paging"].ToString()) == -1)
            Session["paging"] = 0;
        int number_page = Int32.Parse(Session["paging"].ToString());
        int[,] arr = new int[900, 900];

        DataTable dt_search = (DataTable)Session["datatable_search"];
        int page_size = grv.PageSize;
        int start_count = page_size * number_page + 1;

        for (int i = 0; i < grv.Rows.Count; i++)
        {
            GridViewRow row = grv.Rows[i];
            DropDownList ddl = ((DropDownList)row.FindControl("ddl_結果"));
            arr[number_page, start_count] = Int32.Parse(ddl.SelectedValue);
            start_count++;
        }

        Session["arr" + number_page] = arr;
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "startup();", true);

        DropDownList ddl2 = (DropDownList)sender;
        ddl2.Focus();
    }

    /// <summary>
    /// Ham lay loi duoc luu trong database
    /// </summary>
    /// <param name="errorid"></param>
    /// <returns></returns>
    public String GetError(string errorid)
    {
        string error = "";
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        if (dt.Rows.Count > 0)
            error = dt.Rows[0][2].ToString();
        return error;
    }
    protected void ddl_組合選択_SelectedIndexChanged(object sender, EventArgs e)
    {
		txt_グループ名.Text = "";
		txt_グループ名.Enabled = false;
		getDD1_組合員企業選択();
        string strHeader = "面接番号,氏名,ID番号,ID,生年月日,結果,面接情報ID";
        ShowGridViewNotClassEmpty(grv, strHeader, "データがありません。");
		ddl_組合員企業選択.Focus();
		ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "startup();", true);
    }
	protected void ddl_組合員企業選択_SelectedIndexChanged(object sender, EventArgs e)
	{
		txt_グループ名.Text = "";
		txt_グループ名.Enabled = false;
		getDD1_面接日();
		string strHeader = "面接番号,氏名,ID番号,ID,生年月日,結果,面接情報ID";
		ShowGridViewNotClassEmpty(grv, strHeader, "データがありません。");
		ddl_面接日.Focus();
		ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regis", "startup();", true);
	}
	protected void ddl_面接日_SelectedIndexChanged(object sender, EventArgs e)
	{
        if (Session["count_paging"] != null && Int32.Parse(Session["count_paging"].ToString()) >= 0)
        {
            for (int i = 0; i <= Int32.Parse(Session["count_paging"].ToString()); i++)
            {
                Session.Remove("arr" + i);
            }
        }

        txt_グループ名.Text = "";
		txt_グループ名.Enabled = false;
		if (ddl_面接日.SelectedIndex != 0)
		{
			int 組合選択 = Int32.Parse(ddl_組合選択.SelectedValue);
			int 組合員企業選択 = Int32.Parse(ddl_組合員企業選択.SelectedValue);
			string 面接日 = ddl_面接日.SelectedValue;

			BindData(組合選択, 組合員企業選択, 面接日);
		}
		else {
			string strHeader = "面接番号,氏名,ID番号,ID,生年月日,結果,面接情報ID";
			ShowGridViewNotClassEmpty(grv, strHeader, "データがありません。");
		}
		ddl_面接日.Focus();
	}

	[System.Web.Services.WebMethod]
	public static string Calendar(string 組合選択, string 組合員企業選択)
	{
		string ca = "";
		面接情報Manager frm = new 面接情報Manager();
		DataTable dt = new DataTable();
		if (組合選択 != "-1")
		{
			dt = frm.frmJ_InterviewResultSearch_面接日(Convert.ToInt32(組合選択), Convert.ToInt32(組合員企業選択));
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow row in dt.Rows)
				{
					ca += row["面接日"].ToString() + ",";
				}
			}
		}
		//ca = ca.Substring(0, ca.Length - 1);
		return ca;
	}
}
